package cn.it.ygh.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.it.ygh.pojo.Agent;
import cn.it.ygh.pojo.Apply;
import cn.it.ygh.pojo.House;
import cn.it.ygh.pojo.Order;
import cn.it.ygh.service.HouseApplyService;


@Controller
public class HouseApplyController {
	@Autowired
	private HouseApplyService houseApplyService;
	
	@RequestMapping(value = "/getUnorderData.do",method = RequestMethod.GET)
	@ResponseBody
	public Map<Object,Object> getOrderData(@RequestParam(value = "pageStart") int pageStart,@RequestParam(value="pageSize") int pageSize,HttpSession session ){
		
		System.out.println(pageStart);
		System.out.println(pageSize);
		
		Agent ag = (Agent)session.getAttribute("ag");
		String agentAccount = ag.getAgentAccount();
		
		List<Order> UnorderList = houseApplyService.getUnorderData((pageStart-1)*pageSize,pageSize,agentAccount);
		int total = houseApplyService.getUnorderTotal(agentAccount);
		Map<Object,Object> info = new HashMap<Object,Object>();
		info.put("data",UnorderList);
		info.put("total",total);
		return info;
	}
	

	@RequestMapping(value = "/getUnapplyData.do",method = RequestMethod.GET)
	@ResponseBody
	public Map<Object,Object> getApplyData(@RequestParam(value = "pageStart") int pageStart,@RequestParam(value="pageSize") int pageSize,HttpSession session){
		
		System.out.println(pageStart);
		System.out.println(pageSize);
		
		Agent ag = (Agent)session.getAttribute("ag");
		String agentAccount = ag.getAgentAccount();
		
		List<Apply> applyList = houseApplyService.getUnapplyData((pageStart-1)*pageSize,pageSize,agentAccount);
		int total = houseApplyService.getUnapplyTotal(agentAccount);
		Map<Object,Object> info = new HashMap<Object,Object>();
		info.put("data",applyList);
		info.put("total",total);
		return info;
	}
	
	@RequestMapping(value="/UnorderListModify.do",method=RequestMethod.POST)
	public ModelAndView updateOrderList(Order order,HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		
		Agent ag = (Agent)session.getAttribute("ag");
		
		String agentAccount = ag.getAgentAccount();
		order.setAgentAccount(agentAccount);
		houseApplyService.updateOrderList(order);
		Order ord = houseApplyService.getOrderById(order.getOrderId());
		houseApplyService.dealSuccess(ord.getHouseId());
		System.out.println(order.getOrderId());
		modelAndView.setViewName("../agents/jsp/UnorderList");
		return modelAndView;
		
	}
	
	@RequestMapping(value="/UnapplyListModify.do",method=RequestMethod.POST)
	public ModelAndView updateApplyList(Apply apply) {
		ModelAndView modelAndView = new ModelAndView();
		houseApplyService.updateApplyList(apply);
		if(apply.getApplyState().equals("确认"))
		{
			Apply aply = houseApplyService.getApplyById(apply.getApplyId());
			House house = houseApplyService.getHouseById(aply.getHouseId());
			Order order = new Order();
			order.setSellerAccount(house.getHouseUserAccount());
			order.setAgentAccount(house.getHouseAgentAccount());
			order.setBuyerAccount(aply.getUserAccount());
			order.setOrderTime(new Date());
			order.setHouseId(aply.getHouseId());
			order.setHouseAddress(house.getHouseAddress());
			order.setHouseProvince(house.getHouseProvince());
			order.setHouseCity(house.getHouseCity());
			order.setOrderState("未成交");
			System.out.println(order);
			houseApplyService.addOrder(order);
		}
		System.out.println(apply.getApplyId());
		modelAndView.setViewName("../agents/jsp/UnapplyList");
		return modelAndView;
		
	}
	
	@RequestMapping(value = "/getAuditingData.do",method = RequestMethod.GET)
	@ResponseBody
	public Map<Object,Object> getAuditingData(@RequestParam(value = "pageStart") int pageStart,@RequestParam(value="pageSize") int pageSize,HttpSession session){
		
		System.out.println(pageStart);
		System.out.println(pageSize);
		
		Agent ag = (Agent)session.getAttribute("ag");
		String agentAccount = ag.getAgentAccount();
		
		List<House> AuditingList = houseApplyService.getAuditingData((pageStart-1)*pageSize,pageSize,agentAccount);
		int total = houseApplyService.getAuditingTotal(agentAccount);
		Map<Object,Object> info = new HashMap<Object,Object>();
		info.put("data",AuditingList);
		info.put("total",total);
		return info;
	}
	@RequestMapping(value="/AuditingHouse.do",method=RequestMethod.POST)
	public ModelAndView AuditingHouse(House house,HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		
		Agent ag = (Agent)session.getAttribute("ag");
		String houseAgentAccount = ag.getAgentAccount();
		house.setHouseAgentAccount(houseAgentAccount);
		houseApplyService.AuditingHouse(house);
		System.out.println(house.getHouseId());
		modelAndView.setViewName("../agents/jsp/AuditingList");
		return modelAndView;
		
	}
	
	
}
